package com.kingsoft.dc.khaos.module.spark.model.exchangefile.control;

import org.apache.hadoop.fs.FileSystem;
import org.apache.hadoop.fs.Path;
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
import org.dom4j.io.OutputFormat;
import org.dom4j.io.XMLWriter;

import java.io.OutputStream;

/**
 * Created by jing on 19/3/27.
 */
public class ControlFile {
    private String dataType;

    private String fileName;

    private String recordNum;

    private String fileSize;

    private String starttimestamp;

    private String endtimestamp;

    public ControlFile(String dataType, String fileName, String recordNum, String fileSize, String starttimestamp, String endtimestamp) {
        this.dataType = dataType;
        this.fileName = fileName;
        this.recordNum = recordNum;
        this.fileSize = fileSize;
        this.starttimestamp = starttimestamp;
        this.endtimestamp = endtimestamp;
    }

    public String getDataType() {
        return dataType;
    }

    public void setDataType(String dataType) {
        this.dataType = dataType;
    }

    public String getFileName() {
        return fileName;
    }

    public void setFileName(String fileName) {
        this.fileName = fileName;
    }

    public String getRecordNum() {
        return recordNum;
    }

    public void setRecordNum(String recordNum) {
        this.recordNum = recordNum;
    }

    public String getFileSize() {
        return fileSize;
    }

    public void setFileSize(String fileSize) {
        this.fileSize = fileSize;
    }

    public String getStarttimestamp() {
        return starttimestamp;
    }

    public void setStarttimestamp(String starttimestamp) {
        this.starttimestamp = starttimestamp;
    }

    public String getEndtimestamp() {
        return endtimestamp;
    }

    public void setEndtimestamp(String endtimestamp) {
        this.endtimestamp = endtimestamp;
    }

    public void writeXml(String fileName, FileSystem fs) {
        Document document = DocumentHelper.createDocument();
        Element transmitContent = document.addElement("transmit-content");
        Element transmitContentFile = transmitContent.addElement("file");

        //
        Element e1 = transmitContentFile.addElement("datatype");
        e1.addText(this.getDataType());

        Element e2 = transmitContentFile.addElement("filename");
        e2.addText(this.getFileName());

        Element e3 = transmitContentFile.addElement("recordnum");
        e3.addText(this.getRecordNum());

        Element e4 = transmitContentFile.addElement("filesize");
        e4.addText(this.getFileSize());

        Element e5 = transmitContentFile.addElement("starttimestamp");
        e5.addText(this.getStarttimestamp());

        Element e6 = transmitContentFile.addElement("endtimestamp");
        e6.addText(this.getEndtimestamp());

        try {
            OutputStream out = fs.create(new Path(fileName));
            OutputFormat format = OutputFormat.createPrettyPrint();
            format.setEncoding("UTF-8");
            XMLWriter xmlWriter = new XMLWriter(out, format);
            xmlWriter.write(document);
            xmlWriter.close();
        } catch (Exception e) {
            e.printStackTrace();
        }
    }

    public static void main(String[] args) {
    }
}
